package dao;

import beans.User;
import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.JDBCUtils;

import java.sql.SQLException;

/**
 * 专门用来操作用户表
 */
public class UserDao {
    /**
     * @param user servlet层传递过来的用户信息
     * @return 查询结果，如果用户名密码正确，返回用户对象，否则返回null;
     */
    public User login(User user) {
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        User exitUser = null;
        try {
            exitUser = qr.query("select * from `user` where username=? and password=?",
                    new BeanHandler<User>(User.class),
                    user.getUsername(), DigestUtil.md5Hex(user.getPassword()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return exitUser;
    }
}
